<template>
  <div
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    class="arbox"
  >
    <my-scroll>
      <div class="box">
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input
          v-model="keyword"
          placeholder="请输入关键字"
          style="width:30%"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="onSearch"
          >搜索</el-button
        >
        <el-table :data="tableData" border style="width: 100%" stripe>
          <el-table-column prop="title" label="标题"></el-table-column>
          <el-table-column prop="date" label="发表时间"></el-table-column>
          <el-table-column
            prop="category"
            label="分类"
            style="marginRight:5px"
          ></el-table-column>
          <el-table-column prop="views" label="浏览量"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                >查看</el-button
              >
              <el-button @click="handleEdit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button
                @click="handleDelete(scope.row)"
                type="text"
                size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            layout="total ,prev, pager, next ,jumper"
            :total="count"
            :page-size="mount"
          ></el-pagination>
        </div>
      </div>
    </my-scroll>
  </div>
</template>
<script>
// import { request, DeleteArticle } from "../../api/index"
import request from "../../api/request"
export default {
  data() {
    return {
      loading: true,
      keyword: "",
      data: [], //储存所有数据的数组
      tableData: [], //存储当前页数据的数组
      count: null, //总页数
      mount: 8, //每页展示数据的条数
      currentpage: 1,
      options: [
        {
          value: "category",
          label: "按分类查找",
        },
        {
          value: "title",
          label: "按标题查找",
        },
      ],
      value: "title",
    }
  },
  created() {
    this.getDate({})
  },
  methods: {
    // 点击表格的查看按钮
    handleClick(row) {
      this.$router.push({ name: "article", query: { id: row._id } })
    },
    // 点击修改文章时
    handleEdit(row) {
      this.$EventBus.$emit("alter", row) //修改文章，触发editor组件里的方法
      this.$EventBus.$emit("edit") //打开编辑器，触发Backend.vue页面的方法
    },
    // 删除文章
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await request({
            url: "/article/delete",
            data: { _id: row._id },
            method: "post",
          })
          if (res.status) {
            this.$notify({
              title: "成功",
              message: res.msg,
              type: "success",
            })
            this.getDate({})
          } else {
            this.$notify.error({
              title: "错误",
              message: res.msg,
            })
          }
        })
        .catch(() => {
          this.$notify.info({
            title: "消息",
            message: "已取消删除操作",
          })
        })
    },

    // 当页码发生改变时
    handleCurrentChange(val) {
      this.currentpage = val
      this.currentData()
    },

    // 点击搜索按钮时
    onSearch() {
      const data = this.keyword == "" ? {} : { [this.value]: this.keyword }
      this.getDate(data)
    },

    //获取文章的方法
    async getDate(data) {
      let res = await request({
        url: "/article/get",
        data,
        method: "get",
      })
      this.data = []
      this.data = Object.freeze(res.data)
      this.count = res.count
      this.currentData()
      this.loading = false
    },

    // 筛选出当前页展示的数据的方法
    currentData() {
      let table = this.data.filter((item, index) => {
        return (
          index < this.mount * this.currentpage &&
          index >= this.mount * (this.currentpage - 1)
        )
      })
      this.tableData = Object.freeze(table)
    },
  },
}
</script>
<style lang="scss" scoped>
.arbox {
  height: 100%;
  width: 100%;
  overflow: hidden;
}
.box {
  padding: 20px;
}
.pagination {
  margin: 10px 0px;
  text-align: right;
}

.el-input {
  margin: 0px 20px;
  margin-bottom: 20px;
}
.el-select {
  width: 150px;
}
</style>
